require(['../config/requireconfig'],function(config){
	//根据需要添加
	require(['jquery','common/pccommons'],function($,com){
		
        com.init({headerPos:{menu:3,sub:3}});
		var pageLogic = new PageLogic($,com);
		pageLogic.init();
	});
});
var PageLogic = function($,com){
	return{
		init:function(){
			var _this = this;
			_this.type = 1;
			_this.initByUrl();
			_this.loadStateData();
			_this.bindEvents();
			_this.initMap();
		},
		initByUrl:function(){
			var _this = this;
			var obj = {};
			var type = com.utils().urlParam("type");
			if(type){
				_this.type = type;
				obj.type = type;
			}
			var prov = com.utils().urlParam("prov");
			if(prov){
				_this.prov = prov;
				obj.prov = prov;
			}
			var city =  com.utils().urlParam("city");
			if(city){
				_this.city = city;
				obj.city = city;
			}
			var area =  com.utils().urlParam("area");
			if(area){
				_this.area = area;
				obj.area = area;
			}
		},
		bindEvents:function(){
			var _this = this;
			$("#selProvince").on("change",function(){
				if($(this).val() != "0"){
					_this.selectCity($(this).val());
					$("#cityNm").text("");
					_this.loadSysData({
						type:_this.type,
						prov:$(this).val()
					});
				}else{
					_this.clearContent();
				}
			});
			$("#selCity").on("change",function(){
				_this.selectZone($("#selProvince").val(), $(this).val());
				if($(this).val() !="0"){
					$("#cityNm").text($(this).find("option:selected").text());
					_this.loadSysData({
						type:_this.type,
						prov:$("#selProvince").val(),
						city:$("#selCity").val()
					});
				}else{
					_this.loadSysData({
						type:_this.type,
						prov:$("#selProvince").val(),
					});
				}
			});
			$("#selArea").on("change",function(){
				if($(this).val() !="0"){
					_this.loadSysData({
						type:_this.type,
						prov:$("#selProvince").val(),
						city:$("#selCity").val(),
						area:$("#selArea").val()
					});
				}else{
					_this.loadSysData({
						type:_this.type,
						prov:$("#selProvince").val(),
						city:$("#selCity").val()
					});
				}
				
			});
			$("#btnTxt").on("click",function(){
				_this.loadSysData({
					type:_this.type,
					prov:$("#selProvince").val(),
					city:$("#selCity").val(),
					area:$("#selArea").val(),
					name:$("#potName").val()
				});
			});
		},
		loadStateData:function(){
			var _this = this;
			com.ajax({
				url:'/prov.json',
				data:{type:1},
				type:'get',
				success:function(res){
					if(res.success){
						_this.selectProvince(res.data);
					}
				}
			}); 
		},
		selectProvince:function(provs){
			var _this = this;
			$("#selProvince").html(_this.mkOption(provs,"省","prov"));
			$("#selCity").html(_this.mkOption(null,"市","city"));
			$("#selArea").html(_this.mkOption(null,"区","area"));
			if(_this.prov){
				$("#selProvince").val(_this.prov);
				$("#selProvince").trigger("change");
			}
		},
		selectCity:function(code){
			var _this = this;
			if(code == "0"){
				$("#selCity").html(_this.mkOption(null,"市","city"));
				$("#selArea").html(_this.mkOption(null,"区","area"));
				return;
			}
			com.ajax({
				url:'/city.json',
				data:{type:_this.type,prov:code},
				type:'get',
				success:function(res){
					if(res.success){
						var data = res.data;
						$("#selCity").html(_this.mkOption(data,"市","city"));
						$("#selArea").html(_this.mkOption(null,"区","area"));
						if(_this.city && !_this.cityInit){
							$("#selCity").val(_this.city);
							$("#selCity").trigger("change");
							_this.cityInit = 1;
						}
					}
				}
			}); 
		},
		selectZone:function(pCode,citycode){
			var _this = this;
			if(citycode == "0"){
				$("#selArea").html(_this.mkOption(null,"区","area","area"));
				return;
			}
			com.ajax({
				url:'/area.json',
				data:{type:_this.type,prov:pCode,city:citycode},
				type:'get',
				success:function(res){
					if(res.success){
						var data = res.data;
						$("#selArea").html(_this.mkOption(data,"区","area"));
						if(_this.area && !_this.areaInit){
							$("#selArea").val(_this.area);
							$("#selArea").trigger("change")
							_this.areaInit = 1;
						} 
					}
				}
			});
			
		},
		mkOption:function(data,name,col){
			var options = '<option value="0">' + name + '</option>';
			if(data){
				for(var key in data){
					var partNm = col + "Name";
					options += ' <option value="' +  data[key][col] + '">' + data[key][partNm] + '</option>'
				}
			}
			return options;
		},
		loadSysData:function(obj){
			var data = {};
			for(var key in obj){
				if(obj[key] && obj[key]!="0"){
					data[key] = obj[key];
				}
			}
			var _this = this;
			com.ajax({
				url:"/shopQuery.json",
				data:data,
				success:function(res){
					if(res.data){
						_this.genContent(res.data);
					}
				}
			})
		},
		genContent:function(arr){
			var _this = this;
			var html = "";
			$(arr).each(function(){
				html +='<li class="res-item" longitude="' + this.longitude + '" latitude="' + this.latitude + '" >';
				html +='	<i class="tg-ico"></i>';
				html +='	<div class="res-item-word">';
				html +='		<span class="res-item-pie">' + this.name + '</span>';
				html +='		<span class="res-item-pie">' + this.detail + '</span>';
				html +='		<span class="dotline" >查看地图</span>';
				html +='	</div>';
				html +='</li>';
			});
			$("#mapItems").html(html);
			$("#mapItems").find("li").on("click",function(){
				var new_point = new BMap.Point($(this).attr("longitude"),$(this).attr("latitude"));
				var spans = $(this).find(".res-item-word").find(".res-item-pie");
				var content = '';
				$(spans).each(function(){
					content += "<span>" + $(this).html() + "</span><br>";
				});
				var marker = new BMap.Marker(new_point);  // 创建标注
				var infoWindow = new BMap.InfoWindow(content);
				marker.addEventListener("click", function(){    
					 this.openInfoWindow(infoWindow);
				});
				_this.map.centerAndZoom(marker,14);
				_this.map.addOverlay(marker);              // 将标注添加到地图中
				_this.map.panTo(new_point);
			})
		},
		clearContent:function(){
			$("#mapItems").html("");
		},
		initMap:function(){
	      var _this = this;
	      _this.createMap();//创建地图
	    },
	    createMap:function(){
	    	var _this = this;
			_this.map = new BMap.Map("bdMap");
			_this.map.centerAndZoom(new BMap.Point(118.8028910000,32.0647350000),11);
			// 添加带有定位的导航控件
			var navigationControl = new BMap.NavigationControl({
				// 靠左上角位置
				anchor: BMAP_ANCHOR_TOP_LEFT,
				// LARGE类型
				type: BMAP_NAVIGATION_CONTROL_LARGE,
				// 启用显示定位
				enableGeolocation: true
			});
			_this.map.addControl(navigationControl);
			var overView = new BMap.OverviewMapControl();
			var overViewOpen = new BMap.OverviewMapControl({isOpen:true, anchor: BMAP_ANCHOR_BOTTOM_RIGHT});
			_this.map.addControl(overView);          //添加默认缩略地图控件
			_this.map.addControl(overViewOpen);      //右下角，打开

	    }
	}
}